﻿window.Public = window.Public || new Object();
Public.GetLhgInstance = function (title, url, html, width, height, btnBar) {
    var l = new $.dialog({
        title: title,
        html: html,
        page: url,
        width: width,
        height: height,
        iconTitle: false,
        rang: true,
        resize: false,
        btnBar: btnBar,
        cover: true,
        maxBtn: false,
        cancelBtn: true
    });
    return l;
};
//打开一个弹出窗口
//参数说明
//title:显示的标题
//url:打开窗口的地址
//width:窗口的宽度
//height:窗口的高度  
//fun:一个回调函数,在点击窗口右上角的X按钮会调用,不会传递任何参数
//isChild:是否在子窗口打开,即:是否是二级窗口.注意,暂时只支持二级窗口
Public.ShowWindow = function (title, url, width, height, fun, isChild, cancelFun) {
    var t = title ? title : "窗口";
    var l;
    if (isChild) {
        l = this.GetChildLhgInstance(t, url, null, width, height, true);
    }
    else {
        l = this.GetLhgInstance(t, url, null, width, height, true);
    }
    l.btnBar = false;
    l.ShowDialog();
    if (typeof (fun) == "function") {
        l.SetXbtn(function () {
            l.cancel();
            fun();
        });
    }
    if (typeof (cancelFun) == "function") {
        l.SetCancelBtn("取消",function () {
            l.cancel();
            cancelFun(); 
        });
    }
    return l;
};
//打开一个自定义内容的弹出窗口
//参数说明
//title:显示的标题
//html:自定义的内容
//width:窗口的宽度
//height:窗口的高度
//fun:一个回调函数,在点击窗口右上角的X按钮会调用,不会传递任何参数
//isChild:是否在子窗口打开,即:是否是二级窗口.注意,暂时只支持二级窗口
Public.ShowContent = function (title, html, width, height, fun, isChild) {
    var t = title ? title : "窗口";

    //var vHeight = this.GetViewHeight();
    //if (height > vHeight - 150) height = vHeight - 150; //最适高度
    var l;
    if (isChild) {
        l = this.GetChildLhgInstance(t, null, html, width, height, false);
    }
    else {
        l = this.GetLhgInstance(t, null, html, width, height, false);
    }
    l.ShowDialog();
    if (typeof (fun) == "function") {
        l.SetXbtn(function () {
            l.cancel();
            fun();
        })
    }
    return l;
}
//在弹出窗口中得到父窗口对象
//注意,不建议直接用window.parent获取,因为此弹出窗口是跨框架的,应该尽量调用此方法
Public.GetParentWindow = function () {
    if (frameElement.lhgDG) {
        return frameElement.lhgDG.curWin;
    }
};
Public.GetIframeParentWindow = function () {
    if (frameElement.lhgDG) {
        return frameElement.lhgDG.parent.dgWin;
    }
};
//在弹出窗口中,关闭本窗口实例
Public.CloseWindow = function () {
    if (frameElement.lhgDG) {
        frameElement.lhgDG.cancel();
    }
};
Public.GetChildLhgInstance = function (title, url, html, width, height, btnBar, SetId) {
    var DG = frameElement.lhgDG;
    var tmpID = "";
    if (SetId != undefined && SetId != "") {
        tmpID = SetId;
    }
    var l = new DG.curWin.$.dialog({
        id: "child" + tmpID,
        title: title,
        html: html,
        page: url,
        width: width,
        height: height,
        iconTitle: false,
        rang: true,
        resize: false,
        btnBar: btnBar,
        cover: true,
        parent: DG,
        maxBtn: false,
        cancelBtn: true
    });
    return l;
};
//尽量不要调用了,上面的ShowWindow最后个参数可以控制是否在子窗口中打开
Public.ShowChildWindow = function (title, url, width, height, SetId, IsBtn, fun) {
    var t = title ? title : "窗口";
    var l = this.GetChildLhgInstance(t, url, null, width, height, IsBtn, SetId);
    l.btnBar = false;
    l.ShowDialog();
    if (typeof (fun) == "function") {
        l.SetXbtn(function () {
            fun();
            l.cancel();
        });        
    }
    return l;
};
//打开一个选择公共分类的选择窗口
//参数说明
//call:调用码
//width:窗口宽度
//height:窗口高度
//fun:一个回调函数,在点击窗口右上角的X按钮会调用,不会传递任何参数
//isChild:是否在子窗口打开,即:是否是二级窗口.注意,暂时只支持二级窗口
Public.ShowCommonType = function (call, width, height, fun, isChild) {
    var l;
    if (isChild) {
        l = this.GetChildLhgInstance("选择分类", "/Manage/CommonType.aspx?call=" + call, null, width, height, true);
    } else {
        l = this.GetLhgInstance("选择分类", "/Manage/CommonType.aspx?call=" + call, null, width, height, true);
    }
    l.ShowDialog();
    l.addBtn("btnAdd", "确定", function () {
        var select = l.selectTreeValue;
        fun(select);
        l.cancel();
    });
};

Public.ShowBtnWindow = function (title, url, width, height, fun, isChild) {
    var t = title ? title : "窗口";
    var l;
    if (isChild) {
        l = this.GetChildLhgInstance(t, url, null, width, height, false);
    }
    else {
        l = this.GetLhgInstance(t, url, null, width, height, true);
    }
    l.btnBar = true;
    l.ShowDialog();

};
//防止弹出窗口中的tabs出现异常,需要调用此方法重新初始化
Public.Tabs = function (div) {
    setTimeout(function () {
        div.tabs({
            width: "auto",
            height: "auto"
        }).tabs("resize");
    }, 200);
};
//将JSON对象输出为字符串
Public.JsonToString = function (o) {
    if (o == undefined) {
        return "";
    }
    var r = [];
    if (typeof o == "string") return "\"" + o.replace(/([\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";
    if (typeof o == "object") {
        if (!o.sort) {
            for (var i in o)
                r.push("\"" + i + "\":" + this.JsonToString(o[i]));
            if (!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)) {
                r.push("toString:" + o.toString.toString());
            }
            r = "{" + r.join() + "}"
        } else {
            for (var i = 0; i < o.length; i++)
                r.push(this.JsonToString(o[i]));
            r = "[" + r.join() + "]";
        }
        return r;
    }
    return o.toString().replace(/\"\:/g, '":""');
};
//根据模板文件,将指定的对象转换为对应的文本
Public.ReplaceTemplate = function (obj, str) {
    for (var property in obj) {
        var tmpl = "{\\$" + property + "}";
        var reg = new RegExp(tmpl, "g");
        str = str.replace(reg, obj[property]);
    }
    return str;
};
//根据模板文件,将指定的对象集合转换为对应的文本
Public.ReplaceTemplateArray = function (list, str) {
    var result = "";
    $.each(list, function (i, o) {
        result += Public.ReplaceTemplate(o, str);
    });
    return result;
};
//用于刷新UpdatePanel下的分页表格
Public.RefreshTable = function (pagerID) {
    __doPostBack(pagerID, $("#" + pagerID).find("input:text").val());
};
//调用后台方法
Public.CallBack = function (id, eventArgument) {
    __doPostBack(id, eventArgument);
};
//弹出一个等待中的遮罩
//参数:
//hide:整数,表示过几秒自动隐藏.如果不设置,需要手动调用方法隐藏
Public.ShowLoading = function (hide) {
    if (document.getElementById("dvOverlay")) $("#dvOverlay").remove();
    if (document.getElementById("dvLoading")) $("#dvLoading").remove();
    //背景层
    var dvOverlay = $("<div id='dvOverlay'></div>");
    dvOverlay.css("z-index", 100).css("top", 0).css("left", 0).css("background-color", "#000").css("width", "100%").css("filter", "alpha(opacity=75)").css("-moz-opacity", "0.75").css("opacity", "0.75").css("margin", "0").css("padding", "0");

    //图片层
    var dvLoading = $("<div id='dvLoading'></div>");
    dvLoading.css("height", 13).css("width", 208).css("z-index", 103).css("top", "50%").css("left", "50%").css("margin", "-6px 0 0 -104px");

    dvOverlay.appendTo($("body"));
    dvLoading.appendTo($("body")).show();

    if ($.browser.msie && $.browser.version == "6.0") {  //IE6
        var height = $("html").height();
        dvOverlay.css("position", "absolute").css("height", height);
        var marginTop = 0 - parseInt(dvLoading.height() / 2) + (document.documentElement ? document.documentElement.scrollTop : document.body.scrollTop);
        dvLoading.css("position", "absolute").css("margin-top", marginTop);
    }
    else {
        dvOverlay.css("position", "fixed").css("height", "100%");
        dvLoading.css("position", "fixed");
    }
    dvLoading.html("<img src='/images/loading.gif' style='border:0px;' />");

    window.onscroll = function () { return false; };

    if (hide) {
        setTimeout(function () {
            Public.CloseLoading();
        }, hide * 1000);
    }
};
//移除等待遮罩
Public.CloseLoading = function () {
    $("#dvOverlay").remove();
    $("#dvLoading").remove();
};
//用户添加修改数据后刷新列表
Public.OpertAlert = function (msg, flag, id) {
    alert(msg);
    var pageID;
    if (id != undefined) {
        pageID = id;
    } else {
        pageID = 'spiltPager';
    }
    if (typeof (Public.GetParentWindow()) != "undefined") {

        Public.GetParentWindow().Public.RefreshTable(pageID);
        //parent.alertMsg.correct(msg);
    }
    if (flag) {
        Public.CloseWindow();
    }
};
//得到可见区域的高度
Public.GetViewHeight = function () {
    if (window.innerHeight != window.undefined)//FF
    {
        return window.innerHeight;
    }
    if (document.compatMode == 'CSS1Compat')//IE
    {
        return document.documentElement.clientHeight;
    }
    if (document.body)//other
    {
        return document.body.clientHeight;
    }
    return window.undefined;
};
//得到可见区域的宽度
Public.GetViewWidth = function () {
    var offset = 17;
    var width = null;
    if (window.innerWidth != window.undefined)//FF
    {
        //return window.innerWidth-offset; 
        return window.innerWidth;
    }
    if (document.compatMode == 'CSS1Compat')//IE
    {
        return document.documentElement.clientWidth;
    }
    if (document.body)//other
    {
        return document.body.clientWidth;
    }
    return window.undefined;
};
//得到
Public.Combobox = function (select) {
    $("#"+select).combobox({
        filter: function (text, row) {
            if (row.text.indexOf(text) < 0) return false;
            return true;
        }
    });
};
Public.InitTable = function (tableID) {
    var tb = $("#" + tableID);
    tb.ready(function () {
        tb.find("tr:even() td").each(function () {
            $(this).css("background", "#f5fafe");
            $(this).hover(function () {
                $(this).addClass("hover");
            }, function () {
                $(this).removeClass("hover");
            })
        })
    })
};

Public.SelectAll = function (tempControl) {
    //将除头模板中的其它所有的CheckBox取反 
    var theBox = tempControl;
    xState = theBox.checked;

    elem = theBox.form.elements;
    for (i = 0; i < elem.length; i++)
        if (elem[i].type == "checkbox" && elem[i].id != theBox.id) {
            if (elem[i].checked != xState)
                elem[i].click();
        }
    };



//gridview鼠标移上去变样式
Public.GridViewAddClass = function (obj) {
    $(obj).addClass('over');
}

//gridview鼠标移开后去掉样式
Public.GridViewMoveClass = function (obj) {
    $(obj).removeClass('over');
}

//定义数组的删除方法
Array.prototype.Delete = function (n) {
    if (n < 0) return this;
    else return this.slice(0, n).concat(this.slice(n + 1, this.length));
};
//定义字符串的截取方法
String.prototype.Cut = function (n, addStr) {
    if (this.length < n) return this;
    if (!addStr) addStr = "...";
    return this.substr(0, n) + addStr;
};

//将选择的GUID存放到指定控件
Public.ChangeSelectGuidControl = function(changeID, guidStr, IsCheck) {
    var guidStrList = $("#" + changeID).val();
    if (guidStrList == "") {
        guidStrList = ',';
    }
    if (IsCheck) {
        if (guidStrList.indexOf(',' + guidStr + ',') < 0) {
            $("#" + changeID).val(guidStrList + guidStr + ',');
        }
    } else {
        if (guidStrList.indexOf(',' + guidStr + ',') >= 0) {
            $("#" + changeID).val(guidStrList.replace(',' + guidStr, ''));
        }
    }
}



$.fn.getAllSelectedValue = function () {
    var arr = new Array();
    $(this).find("input[type='checkbox'][checked]").each(function () {
        if (this.value != "all") {
            arr.push("'"+this.value+"'");
        }
    });
    return arr;
}

$.fn.getAllSelectedIntValue = function () {
    var arr = new Array();
    $(this).find("input[type='checkbox'][checked]").each(function () {
        if (this.value != "all") {
            arr.push(this.value);
        }
    });
    return arr;
}

$.fn.singleSelected = function (AllControlName, controlName) {
    var all = $("#" + AllControlName);
    var eThis = this;
    if (!eThis.attr("checked")) all.attr("checked", false);
    else {
        var isAll = true;
        $("#" + controlName).find("input[type='checkbox']").each(function () {
            if (this.name != eThis.name) {
                if (!this.checked) {
                    isAll = false;
                    return false;
                };
            }
        });
        if (isAll)
            all.attr("checked", true);
    }
}



$.fn.SelectAll = function (controlName, writeControl) {
    var xState = $(this).attr("checked");
    $("#" + controlName).find("input[type='checkbox']").each(function () {
        if (this.checked != xState) {
            this.checked = xState;
            if (writeControl != undefined) {
                Public.ChangeSelectGuidControl(writeControl, this.value, xState);
            }
        }
    });

}

$.fn.getTabRowId = function () {
    return $(this).parent().parent().parent().attr('rel');
}


Public.NewGuid = function () {
    function S4() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    }
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}


